package org.graduates.webapp.actions;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.projects.graduates.domain.Person;
import org.projects.graduates.domain.PersonUser;
import org.projects.graduates.domain.User;

@ParentPackage("web")
@Action(results = { @Result(name = "success", type = "json", params = { "includeProperties", "success,person,person.id ,user, user.id" }) })
public class CommitRegistAction extends BaseAction {
	private static final long serialVersionUID = -3125535951446444513L;

	private boolean success = false;

	private User user;

	private Person person;

	public String execute() {

		if (null == user || StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword()) || null == person || StringUtils.isEmpty(person.getShowName()) ) {
			return SUCCESS;
		}

		// 使用系统默认加密算法加密
		user = User.userAfterEncryption(user.getUsername(), user.getPassword());
		
		person.setHeadPortrait("images/portrait.jpg");
		
		this.gradApplication.save(person);
		
		this.gradApplication.save(user);
		
		PersonUser pu = new PersonUser(person,user);
		
		this.gradApplication.save(pu);

		success = true;

		return SUCCESS;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

}
